package com.shanghaionstar.discover.service;

import android.content.Context;

import com.onstar.cn.api.ChangePassService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.mag.ovd.ManagedContentDetailDTO;
import com.onstar.cn.more.response.GetAllContentListResponseType;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by 7828wc on 16/7/21.
 */
public class OVDEmailService extends BaseService<ManagedContentDetailDTO> {
    private Context context;

    public OVDEmailService(Context context) {
        this.context = context;
    }


    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public ManagedContentDetailDTO jsonToBean(String json) {
        return null;
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public Observable<GetAllContentListResponseType> getOvdList() {
        return Observable.defer(new Func0<Observable<GetAllContentListResponseType>>() {
            @Override
            public Observable<GetAllContentListResponseType> call() {
                try {
                    ChangePassService joinmeService = new ChangePassService(Constants.CURRENT_SERVER_URL);
                    RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
                    requestEntity = joinmeService.GetovdMail(requestEntity, ApplicationUtil.getInstance().getSubscriberID(), ApplicationUtil.getInstance().getAccountId(), ApplicationUtil.getInstance().getVin());
                    requestEntity = HttpInjectUtils.injectHttp(OVDEmailService.this.getClass(), "getOvdList", requestEntity);
                    return Observable.just(GsonHelper.fromJson(BusinessUtil.getResponse(getDatas(requestEntity), context),GetAllContentListResponseType.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public Observable<ManagedContentDetailDTO> getOvdDetail(final String contentID) {
        return Observable.defer(new Func0<Observable<ManagedContentDetailDTO>>() {
            @Override
            public Observable<ManagedContentDetailDTO> call() {
                try {
                    ChangePassService joinmeService = new ChangePassService(Constants.CURRENT_SERVER_URL);
                    RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
                    requestEntity = joinmeService.GetovdDetail(requestEntity, ApplicationUtil.getInstance().getSubscriberID(), ApplicationUtil.getInstance().getAccountId(), ApplicationUtil.getInstance().getVin(),contentID);
                    requestEntity = HttpInjectUtils.injectHttp(OVDEmailService.this.getClass(), "getOvdDetail", requestEntity,String.class);
                    return Observable.just(GsonHelper.fromJson( BusinessUtil.getResponse(getDatas(requestEntity), context),ManagedContentDetailDTO.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }
}
